//
//  GalleryDetailTopCell.m
//  WanCaiC8
//
//  Created by tangang on 2018/5/8.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "GalleryDetailTopCell.h"
#import "GalleryModel.h"
#import "MSSBrowseNetworkViewController.h"
@interface GalleryDetailTopCell()<UIScrollViewDelegate>
@property (nonatomic ,strong) UIScrollView  *scrollView;
@end

@implementation GalleryDetailTopCell{
//    CGFloat height;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.contentView.userInteractionEnabled=YES;
    }
    return self;
}

- (void)setDataArr:(NSMutableArray *)dataArr{
    _dataArr=dataArr;
    if (self.scrollView.frame.size.height>40) {
        return;
    }
    
    [self.scrollView removeFromSuperview];
    self.scrollView=nil;
    self.scrollView.frame=CGRectMake(0, 0, SCREEN_WIDTH, self.height+40);
    [self.scrollView setContentOffset:CGPointMake(_index*SCREEN_WIDTH, 0) animated:NO];
    
    for (int i=0; i<dataArr.count; i++) {
        GalleryExtraModel *model=dataArr[i];
        UIView *view=[self viewWithModel:model andIndex:i];
        view.userInteractionEnabled=NO;
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.scrollView.contentSize=CGSizeMake(SCREEN_WIDTH*dataArr.count, 0);
    });
}

- (void)setIndex:(NSInteger)index{
    if (_index!=index) {
        _index=index;
        [self setImageWithTag:index];
        [self.scrollView setContentOffset:CGPointMake(index*SCREEN_WIDTH, 0) animated:YES];
    }
}

- (UIView *)viewWithModel:(GalleryExtraModel *)model andIndex:(NSInteger)index{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(index*SCREEN_WIDTH, 0, SCREEN_WIDTH, self.height+40)];
    [self.scrollView addSubview:view];
    QXLable *labTitle=[[QXLable alloc]initTitle];
    labTitle.text=model.Name;
    labTitle.textAlignment=NSTextAlignmentCenter;
    [view addSubview:labTitle];
    [labTitle setFrame:CGRectMake(0, 5, SCREEN_WIDTH, 20)];
    UIImageView *imageV=[[UIImageView alloc]init];
    imageV.tag=index+100;
    imageV.userInteractionEnabled=YES;
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickBigImage:)];
    [imageV addGestureRecognizer:tap];
    [imageV setFrame:CGRectMake(10, 30, SCREEN_WIDTH-20, self.height)];
    [view addSubview:imageV];
    [self setImageWithTag:0];
    return view;
}




-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView==self.scrollView) {
        if (self.locationChange) {
            float x=scrollView.contentOffset.x;
            NSInteger i=(x/SCREEN_WIDTH);
            _index=i;
            [self setImageWithTag:i];
            self.locationChange(_index);
        }
    }
}

- (void)setImageWithTag:(NSInteger)index{
    UIImageView *imageV=[self.scrollView viewWithTag:index+100];
    WS(weakSelf);
    GalleryExtraModel *model=self.dataArr[index];
    [imageV sd_setImageWithURL:[NSURL URLWithString:model.Picture] placeholderImage:[UIImage imageNamed:@"big_loding"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        float   height=SCREEN_WIDTH/image.size.width*image.size.height;
        if (weakSelf.height<height) {
            weakSelf.height=height;
            [imageV setFrame:CGRectMake(10, 30, SCREEN_WIDTH-20, height)];
            weakSelf.scrollView.frame=CGRectMake(0, 0, SCREEN_WIDTH, height+40);
            if (weakSelf.changeHeight) {
                weakSelf.changeHeight(height);
            }
        }
    }];
}


- (void)onclickBigImage:(UITapGestureRecognizer *)tap{
    GalleryExtraModel *model=self.dataArr[tap.view.tag];
    MSSBrowseModel *browseItem = [[MSSBrowseModel alloc]init];
    browseItem.smallImageView = (UIImageView *)tap.view;// 小图
    NSString *strImageUrl=model.Picture;
    if ([strImageUrl containsString:@"_Min"]) {
        strImageUrl = [strImageUrl stringByReplacingOccurrencesOfString:@"_Min" withString:@""];
    }
    browseItem.bigImageUrl =strImageUrl;
    // 加载网络图片大图地址
    MSSBrowseNetworkViewController *bvc = [[MSSBrowseNetworkViewController alloc]initWithBrowseItemArray:@[browseItem] currentIndex:0];
    [bvc showBrowseViewController];
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView=[[UIScrollView alloc]init];
        _scrollView.pagingEnabled=YES;
        _scrollView.showsHorizontalScrollIndicator=NO;
        _scrollView.delegate=self;
        _scrollView.scrollEnabled=YES;
        [self.contentView addSubview:self.scrollView];
//        [self.contentView addGestureRecognizer:self.scrollView.panGestureRecognizer];
    }
    return _scrollView;
}

@end
